package net.dgg.liuyinglong.plugin.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtilBase;
import net.dgg.liuyinglong.plugin.ClassTools;
import net.dgg.liuyinglong.plugin.forms.PluginDialogRefresh;

/**
 * https://blog.csdn.net/u011976726/article/details/53217238
 * https://blog.csdn.net/guohaiyang1992/article/details/79019094
 * https://www.jianshu.com/p/5efcb354b473
 https://www.jianshu.com/p/0117d4b1eb00*
 * 官方教材
 * http://www.jetbrains.org/intellij/sdk/docs/welcome.html
 */
public class PluginRefresh extends AnAction {
    private Project mProject;
    private Editor mEditor;
    private PsiElementFactory mFactory;
    /**
     * 当前文件
     */
    private PsiClass mClass;
    private PsiFile mFile;

    private boolean isRefresh;
    private boolean isLoadMore;
    private boolean isHelperView;
    private String refresh;
    private String recview;
    private String helperView;

    @Override
    public void actionPerformed(AnActionEvent e) {
        mProject = e.getProject();
        mEditor = e.getData(PlatformDataKeys.EDITOR);
        mFile = PsiUtilBase.getPsiFileInEditor(mEditor, mProject);
        mClass = ClassTools.getTargetClass(mEditor, mFile);
        // 获取Factory
        mFactory = JavaPsiFacade.getElementFactory(mProject);
        initView();
    }

    /**
     * 初始化弹出窗口
     */
    private void initView() {
        PluginDialogRefresh myDialog = new PluginDialogRefresh((isRefresh, isLoadMore, isHelperView, refresh, recview, helperView) -> {
            this.isRefresh = isRefresh;
            this.isLoadMore = isLoadMore;
            this.isHelperView = isHelperView;
            this.refresh = refresh;
            this.recview = recview;
            this.helperView = helperView;

            new WriteCommandAction.Simple(mProject) {
                @Override
                protected void run() throws Throwable {
                    findActivityOrFragment();
                }
            }.execute();

        });
        myDialog.setLocationRelativeTo(null);
        myDialog.setSize(400, 350);
        myDialog.setVisible(true);
    }

    /**
     * 获取主体名称
     *
     * @return
     */
    private String headClassName() {
        String name = "";
        if (null != mClass) {
            name = mClass.getName();
            if (name.contains("Activity")) {
                name = name.replace("Activity", "");
            } else if (name.contains("Fragment")) {
                name = name.replace("Fragment", "");
            } else name = "";
        }
        return name;
    }

    private void findActivityOrFragment() {
        PsiMethod[] tempMethods = mClass.findMethodsByName("trySetupData", false);
        if (tempMethods.length > 0 && tempMethods[0].getBody() != null) {
            PsiCodeBlock methodBody = tempMethods[0].getBody();
            StringBuilder empty = new StringBuilder();
            if (isHelperView) {
                empty.append("mLoadingHelper = LoadingHelper.with(refresh);\n");
                empty.append("mLoadingHelper.setOnRetryClickListener(this);\n");
            }
            empty.append("mPresenter.init();\n");
            empty.append("recview.setLayoutManager(new LinearLayoutManager(fetchContext()));\n");
            empty.append("recview.setAdapter(mPresenter.getAdapter());\n");
            empty.append("refresh.setEnableRefresh(true);\n");
            if (isLoadMore) {
                empty.append("refresh.setEnableLoadmore(true);\n");
                empty.append("refresh.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {\n" +
                        "            @Override\n" +
                        "            public void onLoadmore(RefreshLayout refreshlayout) {\n" +
                        "                mPresenter.onLoadmore();\n" +
                        "            }\n" +
                        "\n" +
                        "            @Override\n" +
                        "            public void onRefresh(RefreshLayout refreshlayout) {\n" +
                        "                mPresenter.onRefresh();\n" +
                        "            }\n" +
                        "        });\n");
            } else {
                empty.append("refresh.setEnableLoadmore(false);\n");
                empty.append("refresh.setOnRefreshListener(new OnRefreshListener() {\n" +
                        "            @Override\n" +
                        "            public void onRefresh(RefreshLayout refreshlayout) {\n" +
                        "                mPresenter.onRefresh();\n" +
                        "            }\n" +
                        "        });\n");
            }
            empty.append("mPresenter.onRefresh();\n");
            methodBody.add(mFactory.createStatementFromText(empty.toString(), tempMethods[0]));
            Messages.showInfoMessage(mProject, methodBody.getText(), "提示");
        }
    }

//    private void findContract() {
//
//    }

//    /**
//     * 重写Class
//     */
//    private void reWrite() {
//        JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(mProject);
//        styleManager.optimizeImports(mFile);
//        styleManager.shortenClassReferences(mClass);
//        new ReformatCodeProcessor(mProject, mClass.getContainingFile(), null, false).runWithoutProgress();
//    }
}
